package com.stadium.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stadium.common.exception.RRException;
import com.stadium.common.utils.Md5Utils;
import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.StringUtils;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.GamePlayerDto;
import com.stadium.dto.SysUserDto;
import com.stadium.entity.GamePlayer;
import com.stadium.entity.SysUser;
import com.stadium.mapper.GamePlayerMP;
import com.stadium.mapper.SysUserMP;
import com.stadium.service.SysUserSV;
import com.stadium.vo.RegisterVo;
import com.stadium.vo.SysUserVo;
import com.stadium.vo.searchVo.SysUserSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysUserSVI implements SysUserSV {
    @Autowired
    private SysUserMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private GamePlayerMP gamePlayerMP;

    @Override
    public List<SysUser> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, SysUser.class);
    }

    @Override
    public void save(SysUserVo sysUserVo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserVo, sysUser);
        if(StringUtils.isEmpty(sysUserVo.getId())){
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            sysUser.setId(id);

        }
        sysUser.setCreateUser(UserUtils.getUserId());
        sysUser.setUpdateUser(UserUtils.getUserId());
        sysUser.setPassword(Md5Utils.hash("000000"));
        mapper.save(sysUser);
    }

    @Override
    public void update(SysUserVo sysUserVo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserVo, sysUser);
        sysUser.setUpdateUser(UserUtils.getUserId());
        mapper.update(sysUser);
    }

    @Override
    public void saveBatch(List<SysUser> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    public List<SysUserDto> query(SysUserSearchVo searchVo) {
        return mapper.query(searchVo);
    }

    @Override
    public SysUserDto get(String id) {
        return mapper.get(id);
    }

    @Override
    public int getCount(SysUserSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

    @Override
    public SysUserDto getUserByLoginName(String loginName) {
        return mapper.getUserByLoginName(loginName,null,null);
    }

    @Override
    public String register(RegisterVo registerVo) throws Exception{
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(registerVo, sysUser);
        String userId = null;
        try {
            SysUserDto userByIdNumber = mapper.getUserByIdNumber(sysUser.getIdNumber());
            if (userByIdNumber != null) {
                throw new RRException("身份证号已注册，请使用身份证号登录");
            }
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            sysUser.setId(id);
            sysUser.setCreateUser(UserUtils.getUserId());
            sysUser.setUpdateUser(UserUtils.getUserId());
            sysUser.setPassword(Md5Utils.hash(sysUser.getPassword()));
            sysUser.setRole("1");
            sysUser.setIsAdmin("0");
            mapper.save(sysUser);
            GamePlayer gamePlayer = new GamePlayer();
            String playerid = UUID.randomUUID().toString().replaceAll("-", "");
            gamePlayer.setId(playerid);
            gamePlayer.setUserId(id);
            gamePlayer.setCreateUser(UserUtils.getUserId());
            gamePlayer.setUpdateUser(UserUtils.getUserId());
            gamePlayerMP.save(gamePlayer);
        } catch (Exception e) {
            throw new RRException(e.getMessage());
        }
        return userId;
    }

    @Override
    public List<GamePlayerDto> getUserByIdNo(String searchValue) {
        return mapper.getUserByIdNo(searchValue);
    }

    @Override
    public SysUserDto getUserByIdNumber(String idNumber) {
        return mapper.getUserByIdNumber(idNumber);
    }

    @Override
    public void changePassword(String id, String newPassword) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setPassword(newPassword);
        mapper.update(sysUser);
    }

}
